package com.lmx.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmx.project.common.ErrorCode;
import com.lmx.project.exception.BusinessException;
import com.lmx.project.model.dto.users.UserCountQueryRequest;
import com.lmx.project.model.dto.users.UserExcle;
import com.lmx.project.model.dto.users.UsersCountVo;
import com.lmx.project.model.dto.users.UsersQueryRequest;
import com.lmx.project.model.entity.Category;
import com.lmx.project.model.entity.Type;
import com.lmx.project.model.entity.Users;
import com.lmx.project.model.vo.UsersVo;
import com.lmx.project.service.CategoryService;
import com.lmx.project.service.TypeService;
import com.lmx.project.service.UsersService;
import com.lmx.project.mapper.UsersMapper;
import com.lmx.project.untils.ExcleUtils;
import com.lmx.project.untils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Lenovo
 * @description 针对表【users(用户表)】的数据库操作Service实现
 * @createDate 2023-04-07 22:36:34
 */
@Service
@Slf4j
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users>
        implements UsersService {


    private String usersessionpre = "user";
    @Resource
    private FileUtil fileUtil;

    @Resource
    private UsersMapper usersMapper;

//    @Resource
//    private CategoryService categoryService;
//
//    @Resource
//    private ExcleUtils excleUtils;


    @Override
    public List<UsersCountVo> getUsersCountVo(UserCountQueryRequest countVo) {

        List<UsersCountVo> usersCountVo = usersMapper.getUsersCountVo(countVo);

        return usersCountVo;


    }

    @Override
    public UsersVo getUserCurrent(HttpServletRequest request) {

        UsersVo attribute = (UsersVo) request.getSession().getAttribute(usersessionpre);
//        stringRedisTemplate.opsForValue().get(usersessionpre+":"+)
        log.info("登录后缓存中的信息{}", attribute);
        String preUrl = fileUtil.getPreUrl();

        if (attribute != null) {
            attribute.setAvatar(preUrl + attribute.getAvatar());
        }
        return attribute;
    }



    @Override
    public Page<UsersVo> getUserVo(UsersQueryRequest usersQueryRequest) {

        Page<Users> UsersPage = new Page<>(usersQueryRequest.getCurrent(), usersQueryRequest.getPageSize());

        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
//        邮箱查
        if (StringUtils.isNotBlank(usersQueryRequest.getEmail())) {
            queryWrapper.eq(Users::getEmail, usersQueryRequest.getEmail());
        }
//        id查
        if (usersQueryRequest.getTel() != null && !usersQueryRequest.getTel().equals("")) {
            queryWrapper.eq(Users::getTel, usersQueryRequest.getTel());
        }
// 姓名查
        if (StringUtils.isNotBlank(usersQueryRequest.getName())) {
            queryWrapper.like(Users::getName, usersQueryRequest.getName());
        }

        if (usersQueryRequest.getTypedm() != null && usersQueryRequest.getTypedm() != 0) {
            queryWrapper.eq(Users::getTypedm, usersQueryRequest.getTypedm());
        }

////        根据类别查询
//        if (usersQueryRequest.getCategoryid() != null && usersQueryRequest.getCategoryid() != 0) {
//            queryWrapper.eq(Users::getCategoryid, usersQueryRequest.getCategoryid());
//        }
////        学校查询
//        if (usersQueryRequest.getSchool() != null && !usersQueryRequest.getSchool().equals("")) {
//            queryWrapper.like(Users::getSchool, usersQueryRequest.getSchool());
//        }
//        年级查询
        if (usersQueryRequest.getGrade() != null && !usersQueryRequest.getGrade().equals("")) {
            queryWrapper.like(Users::getGrade, usersQueryRequest.getGrade());
        }
//        班级查询
        if (usersQueryRequest.getLesson() != null && !usersQueryRequest.getLesson().equals("")) {
            queryWrapper.like(Users::getLesson, usersQueryRequest.getLesson());
        }
//     多条件查询是并列关系

        Page<Users> page = page(UsersPage, queryWrapper);
//        if (!page.getRecords().isEmpty()){
        String preUrl = fileUtil.getPreUrl();
        List<UsersVo> collect = page.getRecords().stream().map(users -> {
            UsersVo usersVo = new UsersVo();
            BeanUtils.copyProperties(users, usersVo);

            usersVo.setAvatar(preUrl + usersVo.getAvatar());
            return usersVo;
        }).collect(Collectors.toList());
//        }
        Page<UsersVo> usersVoPage = new Page<>();

        BeanUtils.copyProperties(page, usersVoPage);
        usersVoPage.setRecords(collect);
        return usersVoPage;
    }
}




